<?php

class Tag_IndexController extends Zend_Controller_Action
{

    public function init()
    {
        Zend_Registry::get('layout')->setLayout('duty');
        
        $activeResource=Application_Model_Tags::RESOURCE_NAME;
        
        // seting active resource
        Zend_Registry::set('resource',$activeResource);

        $this->view->headTitle()->append(Zend_Registry::get('locale')->_('Tag'));
        
        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->view->flash_messages = $this->_flashMessenger->getMessages();
    }

    public function indexAction()
    {
        if (!Zend_Registry::get('user')->canView())
        {
            return $this->_response->setRedirect(Zend_Registry::get('view')->fixUrl());
        }
        
        $text=$this->_getParam('tag');
        
        $results=array();
        
        $search=new Search_Model_Search();

        if (!empty($text))
        {
            $results=$search->find($text);
            
            if (empty($results))
            {
                $this->_helper->FlashMessenger(Zend_Registry::get('locale')->_('No documents found'));
             //   return $this->_helper->redirector('index');
                return $this->_response->setRedirect(Zend_Registry::get('view')->fixUrl('search'));
            }
        } else
        {
            return $this->_response->setRedirect(Zend_Registry::get('view')->fixUrl());
        }
        
        if (!empty($results))
        {
            $this->view->headTitle()->append($text);
            
            $results=$search->fetch($results,(int) $this->_getParam('key'),10);
        }
        
        $this->view->results=$this->view->paginator=$results;
    }
    
    /**
     * Tag add action
     */
    public function addAction()
    {
        if (!Zend_Registry::get('user')->canManage())
        {
            return $this->_helper->redirector('index');
        }
        
        $form=new Tag_Form_Tag();
        
        $tagTbl=new Application_Model_TagsDb();

        $request=$this->getRequest();
        
        if ($request->isPost())
        {
            if ($form->isValid($request->getPost()))
            {
                $tag=new Application_Model_Tags(array(
                                                    'tag'=>$form->getValue('tag'),
                                                    'nid'=>0,
                                                    'url_alias'=>'',
                                                    'language'=>Zend_Registry::get('language')->getLocale()
                                                    )
                                                );
                                                
                $tagTbl->save($tag);
        
                $this->_helper->FlashMessenger(Zend_Registry::get('locale')->_('Tag saved'));

                return $this->_response->setRedirect(Zend_Registry::get('view')->fixUrl('tags/add'));                                
            }
        }
        
        $this->view->form=$form;
    }
    
    /**
     * Tag delete action 
     */
    public function deleteAction()
    {
        $id=(int) $this->_getParam('id');
        
        if (empty($id) || !Zend_Registry::get('user')->canManage())
        {
            return $this->_helper->redirector('index');
        }
        
        $tagTbl=new Application_Model_TagsDb();
        $tag=$tagTbl->fetchTagById($id);

        if (!$tag->id)
        {
            return $this->_helper->redirector('index');
        }

        $tagTbl->delete($tag);
        
        
        $this->_helper->FlashMessenger(Zend_Registry::get('locale')->_('Tag was deleted'));

        return $this->_response->setRedirect(Zend_Registry::get('view')->fixUrl('tags'));
    }
}

